package com.kmist.xiakexing.presenters;

import android.nfc.Tag;
import android.util.Log;

import com.kmist.xiakexing.interfaces.IEnemyCallback;
import com.kmist.xiakexing.interfaces.IMainWorldPresenter;
import com.kmist.xiakexing.interfaces.IMainWorldViewCallback;
import com.kmist.xiakexing.interfaces.IMapCallback;
import com.kmist.xiakexing.interfaces.INpcCallback;
import com.kmist.xiakexing.interfaces.IPlayerCallback;
import com.kmist.xiakexing.tools.Config;
import com.kmist.xiakexing.utils.MidEnemyBean;
import com.kmist.xiakexing.utils.scene.EnemyBean;
import com.kmist.xiakexing.utils.scene.GameScene;
import com.kmist.xiakexing.utils.scene.MapBean;
import com.kmist.xiakexing.utils.scene.NpcBean;
import com.kmist.xiakexing.utils.scene.PlayerBean;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


public class MainWorldPresenter implements IMainWorldPresenter, IPlayerCallback, IMapCallback, IEnemyCallback, INpcCallback {

    private static final String TAG = "MainWorldPresenter";
    private List<IMainWorldViewCallback> mCallbacks = new ArrayList<>();
    private GameScene gameScene;
    private PlayerBean player;
    private final List<PlayerBean> playerList;


    private boolean playerLoadedTag = false;
    private boolean mapLoadedTag = false;
    private boolean enemyLoadedTag = false;
    private boolean npcLoadedTag = false;

    private static int errcnt = 0;
    private final UserPreserter userPreserter;
    private final PlayerPresenter playerPresenter;
    private final MapPresenter mapPresenter;
    private final EnemyPresenter enemyPresenter;
    private final NpcPresenter npcPresenter;
    private TaskPresenter taskPresenter;


    private MainWorldPresenter(){

        gameScene = new GameScene();
        playerList = new ArrayList<>();
        gameScene.setPlayerList(playerList);

        userPreserter = UserPreserter.getsInstance();

        playerPresenter = PlayerPresenter.getInstance();
        playerPresenter.registerViewCallback(this);
        player = playerPresenter.getPlayerd();

        mapPresenter = MapPresenter.getmInstance();
        mapPresenter.registerViewCallback(this);

        enemyPresenter = EnemyPresenter.getsInstance();
        enemyPresenter.registerViewCallback(this);

        npcPresenter = NpcPresenter.getsInstance();
        npcPresenter.registerViewCallback(this);

        taskPresenter = TaskPresenter.getInstance();
    }
    private static MainWorldPresenter sInstance = null;

    public static MainWorldPresenter getInstance(){
        if (sInstance == null){
            synchronized (MainWorldPresenter.class){
                if (sInstance == null) {
                    sInstance = new MainWorldPresenter();
                }
            }
        }
        return sInstance;
    }


    @Override
    public void getPropertyInfo() {

    }

    @Override
    public void getGameSence(int newmid) {

        int nenemy = 0;
        long nowtimeStamp = 0;
        MapBean maptmp;

        //player map npc item enemy
        updateLoading(); //发起请求前
        mapPresenter.gotoMap(newmid);
        while(!mapLoadedTag){
            Log.d(TAG, "wait map loaded\n");
        }

        enemyPresenter.getMapEnemy(newmid);
        while(!enemyLoadedTag){
            Log.d(TAG, "wait enemy loaded\n");
        }

        /* 更新地图cd和怪物 */
        nowtimeStamp = System.currentTimeMillis();
        maptmp = gameScene.getMap();

        if(gameScene.getEnemyList() != null){
             nenemy = gameScene.getEnemyList().size();
        }

        try {
            long mgt = Config.dateToStamp(maptmp.getGtime());
            int second = (int)(nowtimeStamp - mgt)%86400;
            Log.d(TAG, "second: "+ second);

            if(second > maptmp.getMs() && nenemy == 0 && maptmp.getGid() != ""){
                //TODO
                mapPresenter.updateMGTime(player.getNowmid());
                enemyPresenter.updateMidEnemy(newmid);
            }
        } catch (ParseException e) {
            //TODO
            e.printStackTrace();
        }

        /* 清空npc列表，然后重新获取，避免出现历史缓存  */
        gameScene.setNpcList(null);
        if(maptmp.getNpc() != "")
        {
            npcPresenter.getNpcList(maptmp.getNpc());
            while(!npcLoadedTag){
                Log.d(TAG, "wait npc loaded\n");
            }
            //添加npc任务列表
            
        }

        enemyPresenter.getMapEnemy(newmid);
        while(!enemyLoadedTag) {
            Log.d(TAG, "wait enemy loaded\n");
        }

        //获取地图玩家
        playerPresenter.getmapPlayer(newmid);
        //获取玩家任务
        //获取npc

        //获取聊天列表


        Log.d(TAG,"gameScene load ok\n");
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultSuccess(gameScene);
        }
        mapLoadedTag = false;
        npcLoadedTag = false;
        enemyLoadedTag = false;

    }

    @Override
    public void getMoreNpcInfo(NpcBean npc) {

        npcPresenter.getNpcMoreInfo(npc);

        /* 绑定npc任务 */
        /* 1. 点开npc名称， 展示npc基本信息， 这个动作默认是对话动作 */
        /* 2. 展示npc当前可执行任务列表 （解析npc tasklist， 调用接口获取任务list， 然后添加到npc对象中，并罗列出任务列表）*/
        /* 3. 点击任务名称，进入任务详情界面 */
    }

    @Override
    public void registerViewCallback(IMainWorldViewCallback callback) {
        if(mCallbacks != null && !mCallbacks.contains(callback)){
            mCallbacks.add(callback);
        }
    }

    @Override
    public void unRegisterViewCallback(IMainWorldViewCallback callback) {
        if (mCallbacks != null) {
            mCallbacks.remove(callback);
        }
    }

    private void updateLoading(){
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultLoading();
        }
    }

    @Override
    public void onPlayerAdded() {
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultSuccess("角色添加成功！");
        }
    }

    @Override
    public void onPlayerAddError() {
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onNetworkError();
        }
    }

    @Override
    public void onPlayerDeled() {
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultSuccess("角色删除成功！");
        }
    }

    @Override
    public void onPlayerDelError() {
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onNetworkError();
        }
    }

    @Override
    public void onPlayerLoaded(PlayerBean playerBean) {
        playerList.add(playerBean);
        playerLoadedTag = true;
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultSuccess("角色读取成功！");
        }
    }

    @Override
    public void onPlayerLoadEmpty() {
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultEmpty();
        }
    }

    @Override
    public void onPlayerLoadErr() {
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultEmpty();
        }
    }

    @Override
    public void onPlayerUpdateOk() {
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultSuccess("角色数据更新成功！");
        }
    }

    @Override
    public void onPlayerUpdateErr() {
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onNetworkError();
        }
    }

    @Override
    public void onPlayerDie() {
        Config.Alarm("你已经重伤请治疗。");
    }

    @Override
    public void onMapLoaded(MapBean mapBean) {
        gameScene.setMap(mapBean);
        mapLoadedTag = true;
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultSuccess("地图数据更新成功！");
        }
    }

    @Override
    public void onMapLoadError() {

    }

    @Override
    public void onMapLoadEmpty() {

    }

    @Override
    public void onEnemyLoaded(EnemyBean enemyBean) {
        //no todo
    }

    @Override
    public void onMapEnemiesLoaded(List<MidEnemyBean> mapEnemys) {
        gameScene.setEnemyList(mapEnemys);
        enemyLoadedTag = true;
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultSuccess("怪物数据更新成功！");
        }
    }

    @Override
    public void onEnemyLoadEmpty() {
        gameScene.setEnemyList(null);
        enemyLoadedTag = true;
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultSuccess("当前地图无怪物！");
        }
    }

    @Override
    public void onEnemyLoadError() {
        //todo
        enemyLoadedTag = true;
    }

    @Override
    public void onNpcLoaded(NpcBean npcBean) {
        // NPC SINGLE NPC GET NOTODO
    }

    @Override
    public void onNpcsLoaded(List<NpcBean> npcBeans) {
        //TODO
        gameScene.setNpcList(npcBeans);
        npcLoadedTag = true;
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultSuccess("NPC数据更新成功！");
        }
    }

    @Override
    public void onNpcLoadEmpty() {
        gameScene.setNpcList(null);
        npcLoadedTag = true;
        for (IMainWorldViewCallback callback : mCallbacks) {
            callback.onResultSuccess("当前地图无NPC！");
        }
    }

    @Override
    public void onNpcLoadError() {
        //TODO
        npcLoadedTag = true;
    }
}
